package com.pratik.gedcom.mapper;

import com.pratik.gedcom.domain.Gedcome;



	public class TokenToGedcomMapper {
	    public Gedcome mapToGedcome(String token) {
	        Gedcome gedcom = new Gedcome();
	        gedcom.setLevel(getLevel(token));
	        gedcom.setTag(getTag(token));
	        gedcom.setId(getId(token));
	        gedcom.setValue(getTagValue(token,gedcom));
	        return gedcom;
	    }


		private String getTagValue(String token, Gedcome gedcom) {
			String tokenWithoutLevelAndTag = token.replace(gedcom.getLevel().toString(), "").replace(gedcom.getTag(), "");
			if(gedcom.getId()!=null ){
				tokenWithoutLevelAndTag=tokenWithoutLevelAndTag.replace(gedcom.getId(), "");
			}
			if(gedcom.getTag()!=null){
				tokenWithoutLevelAndTag=tokenWithoutLevelAndTag.replace(gedcom.getTag().toUpperCase(), "");
			}
			return tokenWithoutLevelAndTag.trim().equals("")?null:tokenWithoutLevelAndTag.trim();
		}


		private String getId(String token) {
	        String tagOrId1=getSecondToken(token);
	        String tagOrId2=getThirdToken(token);
	        if(isId(tagOrId1)){
	        	return tagOrId1;
	        }
	        if(isId(tagOrId2)){
	        	return tagOrId2;
	        }
	        return null;
	    }

	    private String getThirdToken(String token) {
	    	int firstSpaceIndex = token.indexOf(" ");
	    	if(firstSpaceIndex >0){
	    		String tokenWithotLevel = token.substring(firstSpaceIndex).trim();
	    		if(tokenWithotLevel.indexOf(" ")!=-1){
	    			String tokenAtThirdLevel = tokenWithotLevel.substring(tokenWithotLevel.indexOf(" ")).trim();
	    			int endOfThirdToken = tokenAtThirdLevel.indexOf(" ");
	    			if(endOfThirdToken==-1){
	    				return tokenAtThirdLevel;
	    			}
	    			return tokenAtThirdLevel.substring(0,endOfThirdToken);
	    		}
	    	}
			return null;
		}

		private String getSecondToken(String token) {
	    	int firstSpaceIndex = token.indexOf(" ");
	    	if(firstSpaceIndex >0){
	    		String tokenWithotLevel = token.substring(firstSpaceIndex).trim();
	    		int secondTokenEndIndex = tokenWithotLevel.indexOf(" ")==-1?tokenWithotLevel.length():tokenWithotLevel.indexOf(" ");
	    		return tokenWithotLevel.substring(0,secondTokenEndIndex);
	    	}
	    	return null;
		}

		private boolean isId(String tagOrId) {
			if(tagOrId==null){return false;}
	       return tagOrId.indexOf('@')==0&&tagOrId.lastIndexOf('@')==tagOrId.length()-1;
	    }
		
		private boolean isTag(String tagOrId) {
			if(tagOrId==null){return false;}
		  return tagOrId.toUpperCase().equals(tagOrId);
		}

	    private String getTag(String token) {
	    	  String tagOrId1=getSecondToken(token);
		        String tagOrId2=getThirdToken(token);
		        if(!isId(tagOrId1)&&isTag(tagOrId1)){
		        	return tagOrId1.toLowerCase();
		        }
		        if(!isId(tagOrId2)&&isTag(tagOrId2)){
		        	return tagOrId2.toLowerCase();
		        }
		        return null;
	    }


	    private Integer getLevel(String token) {
	        int endIndex = token.indexOf(" ");
	        if(endIndex==-1) {
	            return Integer.parseInt(token);
	        }
	        return Integer.parseInt(token.substring(0, endIndex));
	    }
	}



